<template>
	<el-header style="height: auto;">
		<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
	</el-header>
	<el-header>
		<div class="left-panel"></div>
		<div class="right-panel">
			<div class="right-panel-search">
				<el-input v-model="search.keyword" placeholder="标题 / 摘要" clearable></el-input>
				<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
			</div>
		</div>
	</el-header>
	<el-main class="nopadding">
		<scTable ref="table" :apiObj="list.apiObj" row-key="noticeId" stripe remoteSort remoteFilter>
			<el-table-column label="#" type="index" width="50"></el-table-column>
			<el-table-column label="通知公告标题" prop="noticeTitle" width="200" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column label="摘要" prop="subheading" width="300" align="center" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column label="公告类型" prop="noticeTypeName" width="100"></el-table-column>
			<el-table-column label="创建时间" prop="createTime" width="150"></el-table-column>
			<el-table-column label="发布时间" prop="sendTime" width="100"></el-table-column>
			<el-table-column label="结束时间" prop="sendTime" width="100"></el-table-column>
			<el-table-column label="发布人" prop="createUser" width="100"></el-table-column>
			<el-table-column label="操作" fixed="right" align="right" width="160">
				<template #default="scope">
					<el-button-group>
						<el-button text type="primary" size="small" @click="doApproval(scope.row, scope.$index)">审批</el-button>
						<el-button text type="primary" size="small" @click="readNotice(scope.row,scope.$index)">预览</el-button>
					</el-button-group>
				</template>
			</el-table-column>
		</scTable>
	</el-main>
	<noticeDetails v-if="dialog.detailsSave" ref="detailsDialog" @closed="dialog.detailsSave=false"></noticeDetails>
</template>
<script>
import scSelectFilter from '@/components/scSelectFilter'
import noticeDetails from '@/views/info/notice/noticeDetails'
export default {
	name: "Notice Approval",
	components: {
		scSelectFilter,
		noticeDetails
	},
	data() {
		return {
			dialog: {
				detailsSave:false,
			},
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "通知类型",
					key: "noticeType",
					multiple: true,
					options:[{
						label: "全部",
						value: ""
					}]
				}
			],
			list: {
				apiObj: this.$API.info.notice.getNoticeListForApproval
			},
			search: {
				date:'',
				noticeType:'',
				status:'',
				keyword: "",
			}
		}
	},
	mounted() {
		this.getSysDicByCode();
	},
	methods: {
		//搜索
		upsearch(){
			this.$refs.table.upData(this.search)
		},
		filterChange(data){
			this.search.date = data.date;
			this.search.noticeType = data.noticeType;
			this.search.status = data.status;
			this.$refs.table.upData(this.search)
		},
		async getSysDicByCode()
		{
			var res = await this.$API.system.sysDic.getSysDicByCode.get({code:'noticeType'});
			if (res.code == 200) {
				this.filterData[1].options=this.filterData[1].options.concat(res.data);
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		doApproval(row,index)
		{
			this.$confirm('请您审批是否通过?', '提示', {
				distinguishCancelAndClose:true,
				confirmButtonText: '通过',
				cancelButtonText: '不通过',
				type: 'warning'
			}).then(async () => {
				var res = await this.$API.info.notice.approvalNotice.post({noticeId: row.noticeId, status: "1"});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(async (action) => {
				if(action === 'cancel') {
					var res = await this.$API.info.notice.approvalNotice.post({noticeId: row.noticeId, status: "0"});
					if (res.code == 200) {
						this.$refs.table.refresh()
						this.$message.success(res.message)
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			});
		},
		async changeSwitch(val, row) {
			row.status = row.status == '1' ? '0' : '1'
			row.$switch_status = true;
			delete row.$switch_status;
			row.status = val;
			var res = await this.$API.info.notice.setNoticeStatus.post({noticeId: row.noticeId, status: row.status});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async readNotice(row,index)
		{
			this.dialog.detailsSave = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open('setting').setData(row)
			})
		},
		handleChangeSuccess(){
			this.$refs.table.refresh();
		},
	}
}
</script>
